package com.example.homeworkmutualevaluation.Dao;

import com.example.homeworkmutualevaluation.Entity.Evaluation;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface EvaluationDao extends JpaRepository<Evaluation, Integer> {
    @Transactional
    @Modifying
    @Query(value = "INSERT INTO evaluation(`taskid`, `score`, `comment`) VALUES (?1, ?2, ?3)", nativeQuery = true)
    int insert(int taskid, int score, String comment);

    @Query(value = "SELECT evaluation.* FROM evaluation, task WHERE " +
            "evaluation.`taskid` = task.`id` AND task.`homeworkid`=?1 AND task.`submitter`=?2", nativeQuery = true)
    List<Evaluation> findAllBySubmitter(int homeworkid, String submitter);

    @Query(value = "SELECT evaluation.* FROM evaluation, task WHERE " +
            "evaluation.`taskid` = task.`id` AND task.`homeworkid`=?1 AND task.`evaluator`=?2", nativeQuery = true)
    List<Evaluation> findAllByEvaluator(int homeworkid, String evaluator);

    @Query(value = "SELECT evaluation.* FROM evaluation, task WHERE " +
            "evaluation.`taskid` = task.`id` AND task.`evaluator`=?1", nativeQuery = true)
    List<Evaluation> findAllByEvaluator(String evaluator);
}
